package com.wang.utils;

import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

public class TransactionUtil {

    /**
     * 开启一个事务
     */
    public static TransactionStatus createTransaction(DataSourceTransactionManager transactionManager) {
        //定义事务属性
        DefaultTransactionDefinition transactionDefinition = new DefaultTransactionDefinition();
        transactionDefinition.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
        transactionDefinition.setIsolationLevel(TransactionDefinition.ISOLATION_DEFAULT);
        //开启事务
        return transactionManager.getTransaction(transactionDefinition);
    }
}
